// pages/doctor-goods/doctor-goods.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageSize: 5,
    pageNum: 1,
    goodslist: [],
    imgurl: 'https://tyc-bj.cn/yc/api/attach/',
    upstate: 0,
    downstate: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log('添加完成')
    this.loadData();
  },

  // 加载医美产品数据
  loadData: function(more) {
    console.log('产品', wx.getStorageSync('userid'));
    var that = this;
    wx.request({
      url: app.globalData.baseUrl + 'doctor/product/getPage',
      data: {
        'pageSize': that.data.pageSize,
        'pageNum': that.data.pageNum,
        'doctorId': wx.getStorageSync('userid'),
        // 'doctorId': '6a2fba9822c644ff9556cd3308787d74',
      },
      header: {
        'sign': app.globalData.sign,
        'token': app.globalData.token,
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
      method: 'POST',
      success(res) {
        console.log('医生端医美产品列表:', res);
        wx.hideLoading();
        if (more) {
          if (res.data.data.length) {

            var goodlist = that.data.goodslist.concat(res.data.data);
            that.setData({
              'goodslist': goodlist,
            })
          } else {
            wx.showToast({
              title: '已无更多...',
              icon: 'none',
              duration: 2000,
            });
          }
        } else {
          that.setData({
            'goodslist': res.data.data,
          })
        }

      }
    })
  },

  // 上下架商品接口
  updateState: function(state, id) {
    console.log('上下架:', state, id);
    var that = this;
    wx.request({
      url: app.globalData.baseUrl + 'doctor/product/updateState',
      data: {
        'goodId': id,
        'type': state,
      },
      header: {
        'sign': app.globalData.sign,
        'token': app.globalData.token,
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
      method: 'POST',
      success(res) {
        console.log('上下架结果:', res);
        that.setData({
          'pageSize': 5,
          'pageNum': 1,
        })
        that.loadData();
      }
    })
  },

  // 处理上架逻辑
  handleUp: function(e) {
    var that = this;
    wx.showModal({
      title: '注意!',
      content: '确认上架?',
      confirmText: '是',
      cancelText: '否',
      success: function(res) {
        if (res.cancel) {} else {
          //点击确定 
          var goodsid = e.target.dataset.goodsid;
          that.updateState(1, goodsid);
        }
      },
      fail: function(res) {},
    })
  },

  // 处理下架逻辑
  handleDown: function(e) {
    var that = this;
    wx.showModal({
      title: '注意!',
      content: '确认下架?',
      confirmText: '是',
      cancelText: '否',
      success: function(res) {
        if (res.cancel) {} else {
          //点击确定 
          var goodsid = e.target.dataset.goodsid;
          that.updateState(2, goodsid);
        }
      },
      fail: function(res) {},
    })
  },

  // 处理删除逻辑
  handleDelete: function(e) {
    var that = this;
    wx.showModal({
      title: '注意!',
      content: '确认删除?',
      confirmText: '是',
      cancelText: '否',
      success: function(res) {
        if (res.cancel) {} else {
          //点击确定 
          var goodsid = e.target.dataset.goodsid;
          that.updateState(3, goodsid);
        }
      },
      fail: function(res) {},
    })
  },

  // 处理编辑
  handleEdit: function(e) {
    console.log('编辑商品id:', e)
    var goodsid = e.target.dataset.goodsid;
    // wx.navigateTo({
    //   url: '../doctor-goods-edit-2/doctor-goods-edit-2?choosed=' + this.data.choosed + '&choosedid=' + this.data.choosedid + '&id=' + goodsid,
    // })

    wx.navigateTo({
      url: '../doctor-goods-edit-2/doctor-goods-edit-2?id=' + goodsid,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.loadData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    wx.showLoading({
      title: '加载中...',
    })
    this.setData({
      'pageNum': this.data.pageNum + 1,
    })
    console.log('加载第', this.data.pageNum, '页');
    this.loadData(1);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})